package com.similarworld.bean;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.similarworld.model.Category;
import com.similarworld.model.Item;
import com.similarworld.model.ItemRate;
import com.similarworld.service.CategoryService;
import com.similarworld.service.ItemService;
import com.similarworld.type.CategoryStatus;
import com.similarworld.type.ItemStatus;
import com.similarworld.util.SecurityUtils;

@Component("homeBean")
@Scope("session")
public class HomeBean {

	private static final Logger logger = LoggerFactory.getLogger(HomeBean.class);

	/**************************CATEGORI STAFF STARTS***********************************/
	
	@Autowired
	private CategoryService categoryService;
	
	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	private Category category = new Category();
	private List<Category> categories;
	private Category selectedCategory;
	
	public void saveCategory() {
		if(SecurityUtils.isUserSuperUser() || SecurityUtils.isUserAdmin()) {
			category.setStatus(CategoryStatus.APPROVED);
		} else {
			category.setStatus(CategoryStatus.PENDING);
		}
		categoryService.save(category);
		category = new Category();
		categories = null;
	}

	public List<Category> getCategories() {
		if (categories == null) {
			categories = categoryService.getActiveCategories(500);
		}
		return categories;
	}

	public Category getSelectedCategory() {
		return selectedCategory;
	}

	public void setSelectedCategory(Category selectedCategory) {
		this.selectedCategory = selectedCategory;
	}
	
	/*********************************CATEGORY STAFF ENDS*********************************/
	
	/*********************************ITEM STAFF STARTS*********************************/
	
	@Autowired
	private ItemService itemService;
	
	private Item item = new Item();
	private List<Item> items;
	private Item selectedItem;
	private Integer itemRate;
	
	public void saveItem() {
		item.setStatus(ItemStatus.ACTIVE);
		item.setCategory(selectedCategory);
		item.setCommentable(true);
		item.setRate(5);
		itemService.save(item);
		
		if(itemRate != null) {
			ItemRate rate = new ItemRate();
			rate.setItem(item);
			rate.setRate(itemRate);
			itemService.saveItemRate(rate);
		}
		
		item = new Item();
		items = null;
	}

	public List<Item> getItems() {
		if (items == null) {
			items = itemService.getActiveItems(500);
		}
		return items;
	}

	public Item getSelectedItem() {
		return selectedItem;
	}

	public void setSelectedItem(Item selectedItem) {
		this.selectedItem = selectedItem;
	}

	public Integer getItemRate() {
		return itemRate;
	}

	public void setItemRate(Integer itemRate) {
		this.itemRate = itemRate;
	}

	public Item getItem() {
		return item;
	}

	public void setItem(Item item) {
		this.item = item;
	}

	
	/*********************************ITEM STAFF ENDS*********************************/

}
